package com.suliao.home.serviceImpl;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.suliao.common.util.SMSUtil;
import com.suliao.home.service.AliyunSmsService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: suliao
 * @description: 短信发送
 * @author: Yang-Teng-Fei
 * @create: 2020-05-20 15:25
 **/
@Service
public class AliyunSmsServiceImpl implements AliyunSmsService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private SMSUtil smsUtil;

    @Override
    public Map<String, Object> SmsVerification(String telephone) {
        Map<String, Object> map = new HashMap<>();
        SendSmsResponse sendSmsResponse = smsUtil.sendSms(telephone);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            map.put("code", 200);
            map.put("msg", "短信验证发送成功");
            return map;
        } else {
            map.put("code", 300);
            map.put("msg", sendSmsResponse.getCode());
            return map;
        }
    }

    public Map<String, Object> SmsVerification2(String telephone) {
        List<Map<String, Object>> restrictList = null;

        Map<String, Object> map = new HashMap<>();
        Map<String, SendSmsResponse> result = smsUtil.sendSMS(telephone);
        String code = (String) result.keySet().toArray()[0];
        SendSmsResponse sendSmsResponse = result.get(code);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            map.put("code", 200);
            map.put("msg", "短信验证发送成功");
            map.put("sendCode", code);
            return map;
        } else {
            map.put("code", 300);
            map.put("msg", sendSmsResponse.getCode());
            map.put("sendCode", null);
            return map;
        }

    }
}
